﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Schedule.UI.ScheduleService;
using Schedule.UI.Foundation;
using System.Windows.Input;

namespace Schedule.UI.ViewModel
{
    public class TeacherViewModel : INotifyPropertyChanged
    {
        public TeacherViewModel()
        {
            RefreshGiangvien();
            RefreshLoaiCB();
            RefreshGioiTinh();
            this.PropertyChanged += (s, e) => {
                if (insertCurGiangvien != null)
                    insertCurGiangvien.RaiseCanExecuteChanged();
                if (saveCurGiangvien != null)
                    saveCurGiangvien.RaiseCanExecuteChanged();
                if (delCurGiangvien != null)
                    delCurGiangvien.RaiseCanExecuteChanged();
            };
            
        }
        #region Khai bao
        public ScheduleServiceClient ServiceClient = new ScheduleServiceClient();
        #region propchange
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChange(string propertyName)
        {
            if (PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
        private IEnumerable<GIANGVIEN> giangviens;
        public IEnumerable<GIANGVIEN> Giangviens
        {
            get { return this.giangviens; }
            set
            {
                this.giangviens = value;
                OnPropertyChange("Giangviens");
            }
        }
        private GIANGVIEN currentGiangvien = new GIANGVIEN();
        public GIANGVIEN CurrentGiangvien
        {
            get { return this.currentGiangvien; }
            set
            {
                this.currentGiangvien = value;
                OnPropertyChange("CurrentGiangvien");
                if (this.CurrentGiangvien != null && this.CurrentGiangvien.GiangVienID != 0)
                {
                    CurrentloaiCB = LoaiCBs.Where(x => x.ID == CurrentGiangvien.LoaiCB_ID).First();
                    CurrentGioitinh = Gioitinhs.Where(x => x.ID == CurrentGiangvien.GioiTinh).First();
                }
            }

        }
        private IEnumerable<LOAICB> loaiCBs;
        public IEnumerable<LOAICB> LoaiCBs
        {
            get { return this.loaiCBs; }
            set
            {
                this.loaiCBs = value;
                OnPropertyChange("LoaiCBs");
                if (this.LoaiCBs != null && this.CurrentloaiCB != null && this.CurrentloaiCB.ID == 0)
                    this.CurrentloaiCB = LoaiCBs.First();
            }
        }

        private LOAICB currentloaiCB = new LOAICB();
        public LOAICB CurrentloaiCB
        {
            get { return this.currentloaiCB; }
            set
            {
                this.currentloaiCB = value;
                OnPropertyChange("CurrentloaiCB");
            }

        }

        private IEnumerable<GIOITINH> gioitinhs;
        public IEnumerable<GIOITINH> Gioitinhs
        {
            get { return this.gioitinhs; }
            set
            {
                this.gioitinhs = value;
                OnPropertyChange("Gioitinhs");
                if (this.Gioitinhs != null && this.CurrentGioitinh != null && this.CurrentGioitinh.ID == 0)
                    this.CurrentGioitinh = Gioitinhs.First();
            }
        }

        private GIOITINH currentGioitinh = new GIOITINH();
        public GIOITINH CurrentGioitinh
        {
            get { return this.currentGioitinh; }
            set
            {
                this.currentGioitinh = value;
                OnPropertyChange("CurrentGioitinh");
            }
        }
       
        #endregion

        #region refreshGiangvien
        private void RefreshGiangvien()
        {
            this.ServiceClient.getAllGiangvienCompleted += (s, e) =>
            {
                this.Giangviens = e.Result;
            };
            this.ServiceClient.getAllGiangvienAsync();
        }
        private void RefreshLoaiCB()
        {
            this.ServiceClient.getAllLoaiCBCompleted += (s, e) =>
            {
                this.LoaiCBs = e.Result;
            };
            this.ServiceClient.getAllLoaiCBAsync();
        }
        private void RefreshGioiTinh()
        {
            //throw new NotImplementedException();
            this.ServiceClient.getAllGioiTinhCompleted += (s, e) =>
            {
                this.Gioitinhs = e.Result;
            };
            this.ServiceClient.getAllGioiTinhAsync();
        }
        #endregion
        
        #region Save
        private RelayCommand saveCurGiangvien;
        public ICommand SaveCurGiangvien
        {
            get { return saveCurGiangvien ?? (saveCurGiangvien = new RelayCommand(() => SaveGiangvien(), () => this.currentGiangvien != null && this.currentGiangvien.GiangVienID != 0)); }
        }

        private void SaveGiangvien()
        {
            CurrentGiangvien.LoaiCB_ID = CurrentloaiCB.ID;
            CurrentGiangvien.GioiTinh = CurrentGioitinh.ID;
            ServiceClient.saveGiangvienAsync(currentGiangvien);
            RefreshGiangvien();
        }

        
        #endregion

        #region del
        private RelayCommand delCurGiangvien;
        public ICommand DelCurGiangvien
        {
            get { return delCurGiangvien ?? (delCurGiangvien = new RelayCommand(() => DelGiangvien(), () => this.currentGiangvien != null && this.currentGiangvien.GiangVienID != 0)); }
        }

        private void DelGiangvien()
        {
            ServiceClient.deleteGiangvienAsync(currentGiangvien);
            RefreshGiangvien();
        }
        #endregion

        #region Insert

        private RelayCommand insertCurGiangvien;
        public ICommand InsertCurGiangvien
        {
            get { return insertCurGiangvien ?? (insertCurGiangvien = new RelayCommand(() => InsertGiangvien(), () => this.currentGiangvien != null)); }
        }

        private void InsertGiangvien()
        {
            CurrentGiangvien.LoaiCB_ID = CurrentloaiCB.ID;
            CurrentGiangvien.GioiTinh = CurrentGioitinh.ID;
            ServiceClient.insertGiangvienAsync(CurrentGiangvien);
            RefreshGiangvien();
        }
        #endregion
        
    }
}
